$(function () {
    var columns = [
        [
            {field: 'checked', checkbox: true},
            {title: '物资编码', field: 'wzgg.oldcode', width: 180},
//            {title: '物资编码(新)', field: 'wzgg.code', width: 150},
            {title: '物资名称', field: 'wzgg.name', width: 150},
            {title: '规格型号', field: 'wzgg.ggxh', width: 150},
            {title: '图号', field: 'wzgg.th', width: 150},
            {title: '单位', field: 'wzgg.dw', width: 80},
            {title: '需求计划数量', field: 'jhsl',
                formatter:function(value){
                    var sl = formatSlForHtml(value,",");
                    return sl;
                },width: 150},
            {title: '本站库存数量', field: 'kcsl',
                formatter:function(value){
                    var sl = formatSlForHtml(value,",");
                    return sl;
                },width: 150},
            {title: '一级平衡数量', field: 'sl1', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 4}},
                formatter:function(value){
                    var sl = formatSlForHtml(value,",");
                    return sl;
                },width: 150},
            {title: '上报计划数量', field: 'sbsl', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 4}},
                formatter:function(value){
                    var sl = formatSlForHtml(value,",");
                    return sl;
                },width: 150},
            {title: '退回原因', field: 'thyy', editor: {type: 'text'}, width: 150},
            {title: '到货日期', field: 'dhrq', editor: {type: 'datebox'}, width: 150},
            {title: '牌号', field: 'ph',width:150},
            {title: '设备材料', field: 'sbcl',   formatter: function (value) {
                if(value=='sb'){
                    return "设备";
                }
                if(value =="cl"){
                    return "材料";
                }
            },width: 150},
            {title: '备注', field: 'bz', editor: {type: 'text'}, width: 100},
            {title: '计划员', field: 'createUser.xm', width: 80}
        ]
    ];

    var options = {
        title: $("#bgjhgyz_content_bgjhhz_sm").val(),
        toolbar: '#bgjhgyz_bgjhhzmx_toolbar',
        fitColumns: true,
        columns: columns,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        rownumbers: true,
        url: $.webPath() + '/jhgl/bgjhgyz/bgjhgyzmxList?id=' + $("#bgjhgyz_content_bgjhhz_id").val() + "&zt=1",
        onClickRow: function (rowIndex, rowData) {
            datagridOnSelectHandle(rowIndex, rowData);
        }
//        ,
//        loadFilter: function (data) {
//            if (data.d) {
//                return data.d;
//            } else {
//                return data;
//            }
//        }
    };

    //表格
    var grid = $('#bgjhgyz_bgjhhzmx_edatagrid').edatagrid(options);


    //表格处在编辑状态的时候，给表单绑定事件
    function datagridOnSelectHandle(rowIndex, rowData) {
        //获取每个单元格
        $(grid).edatagrid('editRow', rowIndex);
        var editors = $(grid).datagrid('getEditors', rowIndex);
        if (editors.length == 0)
            return;
        var yjphslEditor = editors[0]; // 一级平衡数量
        var sbjhslEditor = editors[1]; //上报计划数量

        //绑定事件
        if(!rowData['kcsl']){
            rowData['kcsl'] = 0;
        }
        if(rowData['jhsl'] < rowData['kcsl']){
            $(yjphslEditor.target).numberbox({min: 0, max: rowData['jhsl']});
        } else{
            $(yjphslEditor.target).numberbox({min: 0, max: rowData['kcsl']});
        }
        yjphslEditor.target.unbind("blur").bind('blur', function () {
            $(yjphslEditor.target).numberbox('setValue',$(yjphslEditor.target).val());
            $(sbjhslEditor.target).numberbox('setValue',rowData.jhsl - $(yjphslEditor.target).numberbox("getValue"));
        });

        $(sbjhslEditor.target).numberbox({min: 0, max: rowData['jhsl']});
        sbjhslEditor.target.unbind("blur").bind('blur', function () {
            $(sbjhslEditor.target).numberbox('setValue',$(sbjhslEditor.target).val());
            if((rowData.jhsl - $(sbjhslEditor.target).numberbox("getValue")) > rowData['kcsl']){
                $(yjphslEditor.target).numberbox('setValue',rowData['jhsl']);
                $(sbjhslEditor.target).numberbox('setValue',rowData.jhsl - $(yjphslEditor.target).numberbox("getValue"));
            } else{
                $(yjphslEditor.target).numberbox('setValue',rowData.jhsl - $(sbjhslEditor.target).numberbox("getValue"));
            }
        });
        //todo bug
//        yjphslEditor.target.unbind("change").bind('change', function () {
//            var sbjhsl = parseFloat($(sbjhslEditor.target).val());
//            var yjphsl = parseFloat($(this).val());
//            var result = accSub(sbjhsl, yjphsl);
//            if (result < 0) {
//                $(this).numberbox("setValue", sbjhsl);
//                $(sbjhslEditor.target).numberbox("setValue", 0);
//            }
//            $(sbjhslEditor.target).numberbox("setValue", result);
//        });
    }
});